/*USART routines*/
#ifndef USART_H
#define USART_H

/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"

/*Header File*/
#include "semphr.h"
#include "..\common\errcode.h"
#include "..\common\osdata.h"

/*Definitions*/	
#define ERR_NO_SPACE_IN_BUFFER	129 //Module specific error codes

/*Nul terminated receive*/
#define IS_NULL_TERMINATED 0x8000

/*IO Ports definition*/

/*Control Lines definition*/

/*enumerations*/
//Com ports definition
typedef enum
{
	serCOM1 = 0,
	serCOM2
} eCOMPort;

//Error codes for USART initialization
typedef enum
{
	usInitSuccess = 0,
	usInitNullPointer,
	usInitMaxSerCOM,
	usInitAlreadyAlloc,
	usInitNoSpaceForAlloc	
} eUsInitError;

/*Types Definition, unions*/
#pragma anon_unions

//Transmit and receive buffers definition
typedef struct
{
    unsigned portCHAR *data;
    unsigned portSHORT size, head, tail, cntToEvent;
	tOSData  mutex, event;
} tFifo, *ptFifo;

typedef struct
{
    tFifo FifoRx;
	tFifo FifoTx;
} tDataLink;

typedef struct
{
	unsigned portLONG ulBauds;
} tConfig;

//Port Specific data
typedef struct
{
	tConfig xConfig;
	tDataLink xDataLink;
} tUsartPortData, *pUsartPortData;

//Initialization data
typedef struct
{
	unsigned portLONG ulBauds;
	eCOMPort eSerialPort;
	unsigned portSHORT bufferSizeTX, bufferSizeRX; 
} tUsartInitData, *pUsartInitData;

/*Send and receive data objects*/
typedef struct
{
	eCOMPort port;
	unsigned portSHORT size;
	const unsigned portCHAR *data;
	portTickType xBlockTime;
} tUsartSendData, *pUsartSendData;

typedef struct
{
	eCOMPort port;
	unsigned portSHORT size;
	unsigned portCHAR *data;
	portTickType xBlockTime;
} tUsartReceiveData, *pUsartReceiveData;

/*Constants*/

/*Variable Definition*/

/*Functionality enabling*/

/*Public Functions and procedures Declaration*/
extern void vSerialISREntry( void ); //Interrupt entry point written in the assembler file serialISR.s
void vSerialISR(void);
eUsInitError eUsartInit(pUsartInitData pData);
unsigned portCHAR cUSartSend(pUsartSendData pUsart);
unsigned portCHAR cUSartReceive(pUsartReceiveData pUsart);
/*Macro definitions*/
#define bFifoisEmpty(x) 	(x->head == x->tail)
#define vFifoSetEmpty(x) 	(x->tail = x->head)
#define vFifoInit(x) 		 x->tail = x->head = 0
#define nullTerminatedMax(x) (x | IS_NULL_TERMINATED)


#endif //USART_H
